package net.towee.client.com.app.presenter;

import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

import net.towee.client.com.app.ConnectionController;
import net.towee.client.com.app.presenter.ConnectionControllerPresenter.Display;
import net.towee.client.siowrapper.SocketIOClient;
import net.towee.client.siowrapper.SocketRPCController;
import net.towee.client.siowrapper.event.SIOConnectedEvent;
import net.towee.client.siowrapper.event.SIOConnectedEvent.SIOConnectedHandler;
import net.towee.client.siowrapper.event.SIOConnectionFailedEvent;
import net.towee.client.siowrapper.event.SIOConnectionFailedEvent.SIOConnectionFailedHandler;
import net.towee.client.siowrapper.event.SIODisconnectEvent;
import net.towee.client.siowrapper.event.SIODisconnectEvent.SIODisconnectHandler;
import net.towee.client.siowrapper.event.SIOReconnectFailedEvent;
import net.towee.client.siowrapper.event.SIOReconnectFailedEvent.SIOReconnectFailedHandler;
import net.towee.client.siowrapper.event.SIOReconnectedEvent;
import net.towee.client.siowrapper.event.SIOReconnectedEvent.SIOReconnectedHandler;
import net.towee.client.siowrapper.event.SIOReconnectingEvent;
import net.towee.client.siowrapper.event.SIOReconnectingEvent.SIOReconnectingHandler;

public class ConnectionControllerPresenter implements ConnectionController, SIOConnectedHandler,
		SIODisconnectHandler, SIOReconnectedHandler, SIOReconnectingHandler, SIOConnectionFailedHandler,
		SIOReconnectFailedHandler {

	public interface Display extends IsWidget {
		void showDisconnectedMessage();

		void showReconnectingMessage(int dur, int att);

		void showFailureMessage();

		void showConnectedMessage();

		void showConnectingMessage();
	}

	private final Display display;

	@Inject
	public ConnectionControllerPresenter(Display display) {
		this.display = display;
	}

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		
		display.showConnectingMessage();

		// Add listener for connection
		SocketIOClient.getInstance().addSIOConnectedHandler(this);
		SocketIOClient.getInstance().addSIODisconnectHandler(this);
		SocketIOClient.getInstance().addSIOReconnectedHandler(this);
		SocketIOClient.getInstance().addSIOReconnectingHandler(this);
		SocketIOClient.getInstance().addSIOConnectionFailedHandler(this);
		SocketIOClient.getInstance().addSIOReconnectFailedHandler(this);
	}

	@Override
	public void onSIOConnected(SIOConnectedEvent event) {
		display.showConnectedMessage();
	}

	@Override
	public void onSIODisconnect(SIODisconnectEvent event) {
		display.showDisconnectedMessage();
	}

	@Override
	public void onSIOReconnecting(SIOReconnectingEvent event) {
		display.showReconnectingMessage(event.getReconnectionDelay(), event.getReconnectionAttempts());
	}

	@Override
	public void onSIOReconnected(SIOReconnectedEvent event) {
		display.showConnectedMessage();
	}

	@Override
	public void onSIOReconnectFailed(SIOReconnectFailedEvent event) {
		display.showFailureMessage();
	}

	@Override
	public void onSIOConnectionFailed(SIOConnectionFailedEvent event) {
		display.showFailureMessage();
	}
}
